<template>
  <span v-if="text" @click="handleLock">{{ text }}</span>
  <i v-else class="icon-suoping" @click="handleLock"></i>
  <el-dialog title="设置锁屏密码" v-model="box" width="30%" append-to-body>
    <el-form :model="form" ref="formRef" label-width="80px">
      <el-form-item label="锁屏密码" prop="passwd" :rules="[{ required: true, message: '锁屏密码不能为空'}]">
        <el-input v-model="form.passwd" placeholder="请输入锁屏密码">
          <template v-slot:append>
            <el-button @click="handleSetLock" icon="el-icon-lock"></el-button>
          </template>
        </el-input>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import {validatenull} from "utils/validate";
import {useCommonStore} from "../../../store/modules/common.js";
import {toRefs, reactive, computed} from "vue";
import {useRouter} from "vue-router"

defineOptions({
  name: "top-lock",
})

const props = defineProps({
  text: String
})

const optionData = reactive({
  box: false,
  form: {
    passwd: ""
  }
})
const {box, form} = toRefs(optionData)

const lockPasswd = computed(() => {
  const comm = useCommonStore();
  return comm.lockPasswd
})

const formRef = ref()

function handleSetLock() {
  const commonStore = useCommonStore()
  // this.$refs["form"].validate(valid => {
  formRef.value.validate(valid => {
    if (valid) {
      commonStore.SET_LOCK_PASSWD(optionData.form.passwd);
      // this.$store.commit("SET_LOCK_PASSWD", this.form.passwd);
      handleLock();
    }
  });
}

const router = useRouter();

function handleLock() {
  if (validatenull(lockPasswd.value)) {
    optionData.box = true;
    return;
  }
  const commonStore = useCommonStore()
  // this.$store.commit("SET_LOCK");
  commonStore.SET_LOCK();
  setTimeout(() => {
    router.push({path: "/lock"});
  }, 100);
}
</script>

<style lang="scss" scoped>
</style>
